package com.polaris.live.manager

import com.polaris.live.common.util.LogExt
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.LiveConst.LiveState
import im.zego.zegoexpress.constants.ZegoStreamQualityLevel

/**
 * 直播状态管理器
 *
 * @author Created by lucas on 2023/11/13 09:20
 */
object LiveStateManager {

    /**
     * 主播状态
     */
    private var mState = LiveConst.State.NONE

    /**
     * 尝试修改状态
     *
     * 最终以返回值为准
     * @return 最终应该有的状态，为空代表状态没有改变
     */
    fun changeState(@LiveState state: Int): Int? {
        if (mState == state) {
            return null
        }

        when (mState) {
            LiveConst.State.NONE -> {
                //任何状态都可以修改
            }

            LiveConst.State.ONLY_LIVE -> {
                if (state == LiveConst.State.LIVE_AND_MATCH) {
                    return null
                }
            }

            LiveConst.State.LIVE_AND_MATCH -> {
                if (state == LiveConst.State.ONLY_LIVE) {
                    return null
                }
            }

            LiveConst.State.CALL -> {
            }
        }

        return state.apply {
            mState = this
        }
    }

    /**
     * 某些状态需要强制修改
     */
    fun changeStateForce(@LiveState state: Int) {
        //如果不变说明可能是状态出现问题，需要强制修改
        if (changeState(state) == null) {
            LogExt.logI("--live", "stateForceChange: mState $mState, state $state")
            mState = state
        }
    }

    /**
     * 获取状态，最终需要调用[changeState]才能修改状态
     */
    @LiveState
    fun getState(): Int {
        return mState
    }

    /**
     * 是否在正常状态
     */
    fun isNone(): Boolean {
        return mState == LiveConst.State.NONE
    }

    /**
     * 是否在直播中
     */
    fun isLiving(): Boolean {
        return mState == LiveConst.State.ONLY_LIVE || mState == LiveConst.State.LIVE_AND_MATCH
    }

    /**
     * 是否在通话中
     */
    fun isCalling(): Boolean {
        return mState == LiveConst.State.CALL
    }

    /**
     * 是否在通话或者直播
     */
    fun isStarting(): Boolean {
        return isLiving() || isCalling() || isSpectating()
    }

    /**
     * 观众状态
     */
    private var mSpectating = false

    /**
     * 更新观看直播状态
     */
    fun updateSpectateState(spectating: Boolean) {
        mSpectating = spectating
    }

    /**
     * 是否观看直播
     */
    fun isSpectating(): Boolean {
        return mSpectating
    }

    /**
     * 是否充值中
     */
    private var mRecharging = false

    /**
     * 更新充值状态
     */
    fun updateRechargeState(isRecharging: Boolean) {
        mRecharging = isRecharging
    }

    /**
     * 是否充值中
     */
    fun isRecharging(): Boolean {
        return mRecharging
    }

    /**
     * 是否是快捷充值
     */
    private var fastRecharging = false

    /**
     * 更新快捷充值状态
     */
    fun updataFastRechargeState(isFasting: Boolean) {
        fastRecharging = isFasting
    }

    /**
     * 返回快捷充值状态
     */
    fun isFastRecharging(): Boolean = fastRecharging

    /**
     * 用户推拉流质量
     */
    //不好
    val USER_LIVE_STATE_BAD = setOf(
        ZegoStreamQualityLevel.BAD,
        ZegoStreamQualityLevel.DIE,
        ZegoStreamQualityLevel.UNKNOWN
    )

    //好的
    val USER_LIVE_STATE_GOOD = setOf(
        ZegoStreamQualityLevel.EXCELLENT,
        ZegoStreamQualityLevel.GOOD,
        ZegoStreamQualityLevel.MEDIUM
    )
}